package com.zq.it.dao.impl.admin;

import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.zq.it.dao.admin.TeacherDao;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.Teacher;

public class TeacherDaoImpl extends BaseDao<Teacher> implements TeacherDao {

	/* public static final Logger logger=t(TeacherDaoImpl.class); */
	@Override
	public void saveTeacherEty(Teacher academty) {
		getHibernateTemplate().save(academty);
	}
	@Override
	public void deleteTeacherEtyByID(Integer id) {
		deleteEntity("delete Teacher as t where t.teacherId=" + id);
	}
	@Override
	public List findTeacherByHQL(String hql) {
		return getHibernateTemplate().find(hql);
	}
	@Override
	public List<Teacher> findTeacherEtyList() {
		return getHibernateTemplate().find("from Teacher");
		// TODO Auto-generated method stub

	}
	@Override
	public List findTeacherEtyById(Integer id) {
		return getHibernateTemplate().find("from Teacher where teacherId=?",
				id);
		// TODO Auto-generated method stub

	}

	@Override
	public List findTeacherEtyByName(String name) {
		return getHibernateTemplate().find(
				"from Teacher as teacher where teacher.teacherName=?", name);
		// TODO Auto-generated method stub

	}

	// @Override
	// public void updateTeacherByID(Integer id) {
	// getHibernateTemplate().update(entity);
	// // TODO Auto-generated method stub
	//
	// }

	@Override
	public void updateTeacherEty(Teacher teacher) {
		getHibernateTemplate().update(teacher);
		// TODO Auto-generated method stub

	}
	// @Override
	// public List findTeacherEtyByDate(Date date){
	// // TODO Auto-generated method stub
	// return getHibernateTemplate().find("from Teacher where
	// createTime=?",date);
	//
	@Override
	public List findTeacherEtyByExmp(Teacher teacher) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().findByExample(teacher);
	}
	@Override
	public void deleteAllEty(long[] ids) {
		HibernateTemplate ht = getHibernateTemplate();
		for (long i : ids)
			ht.delete(ht.load(Teacher.class, i));
	}
	@Override
	public void deleteTeacherEtyByObj(Teacher teacher) {
		getHibernateTemplate().delete(teacher);
	}
	@Override
	public void saveTeacherEtyList(Collection<Teacher> collection) {
		getHibernateTemplate().saveOrUpdateAll(collection);
	}
	@Override
	public List findTeacherBySql(String sql) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().find(sql);
	}

	private static final Logger log = LoggerFactory
			.getLogger(TeacherDaoImpl.class);
	// property constants
	public static final String TEACHER_NO = "teacherNo";
	public static final String SEX = "sex";
	public static final String NAME = "name";
	public static final String BIRTH = "birth";
	public static final String TITLE = "title";
	public static final String DEGREE = "degree";
	public static final String EMAIL = "email";
	public static final String TEACHER_IMG = "teacherImg";
	public static final String CREATE_TIME = "createTime";
	public static final String EXTEND_FILED = "extendFiled";

	@Override
	public void save(Teacher transientInstance) {
		log.debug("saving Teacher instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void delete(Teacher persistentInstance) {
		log.debug("deleting Teacher instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public Teacher findById(Long id) {
		log.debug("getting Teacher instance with id: " + id);
		try {
			Teacher instance = (Teacher) getHibernateTemplate()
					.get("com.zq.it.pojo.Teacher", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public List findByExample(Teacher instance) {
		log.debug("finding Teacher instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Teacher instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Teacher as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findByTeacherNo(Object teacherNo) {
		return findByProperty(TEACHER_NO, teacherNo);
	}

	@Override
	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	@Override
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	@Override
	public List findByBirth(Object birth) {
		return findByProperty(BIRTH, birth);
	}

	@Override
	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	@Override
	public List findByDegree(Object degree) {
		return findByProperty(DEGREE, degree);
	}

	@Override
	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	@Override
	public List findByTeacherImg(Object teacherImg) {
		return findByProperty(TEACHER_IMG, teacherImg);
	}

	@Override
	public List findByCreateTime(Object createTime) {
		return findByProperty(CREATE_TIME, createTime);
	}

	@Override
	public List findByExtendFiled(Object extendFiled) {
		return findByProperty(EXTEND_FILED, extendFiled);
	}

	@Override
	public List findAll() {
		log.debug("finding all Teacher instances");
		try {
			String queryString = "from Teacher";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@Override
	public void update(Teacher instance) {
		log.debug("attaching dirty Teacher instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	@Override
	public List findByHql(String hql) {
		log.debug("finding Teacher instances by Hql");
		try {
			return getHibernateTemplate().find(hql);
		} catch (RuntimeException re) {
			log.error("find by Hql failed", re);
			throw re;
		}
	}
}
